package com.yunxin.download.server.api;

import com.yunxin.common.result.Result;
import com.yunxin.download.server.api.request.AddDataSourceRequest;
import com.yunxin.download.server.api.request.UpdateDataSourceRequest;
import com.yunxin.download.server.model.entity.DownloadDataSourceConfig;
import com.yunxin.download.server.service.DataSourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author zero°
 * @since 2023-06-13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/data/source")
public class DataSourceController {

    private final DataSourceService dataSourceService;

    @PostMapping("/addDataSource")
    public Result<String> addDataSource(@Validated @RequestBody AddDataSourceRequest request) {
        DownloadDataSourceConfig config = new DownloadDataSourceConfig();
        config.setDataSourceClassName(request.getDataSourceClassName());
        config.setDataSourceName(request.getDataSourceName());
        config.setDataSourceUrl(request.getDataSourceUrl());
        config.setDataSourcePassword(request.getDataSourcePassword());
        config.setDataSourceUserName(request.getDataSourceUserName());
        config.setDataSourceType(request.getDataSourceType());
        config.setSystemCode(request.getSystemCode());
        String key = dataSourceService.addDateSource(config);
        return Result.success(key);
    }

    @PostMapping("/updateDataSource")
    public Result<String> updateDataSource(@Validated @RequestBody UpdateDataSourceRequest request) {
        DownloadDataSourceConfig config = new DownloadDataSourceConfig();
        config.setId(request.getId());
        config.setDataSourceClassName(request.getDataSourceClassName());
        config.setDataSourceName(request.getDataSourceName());
        config.setDataSourceUrl(request.getDataSourceUrl());
        config.setDataSourcePassword(request.getDataSourcePassword());
        config.setDataSourceUserName(request.getDataSourceUserName());
        config.setDataSourceType(request.getDataSourceType());
        config.setSystemCode(request.getSystemCode());
        dataSourceService.updateDateSource(config);
        return Result.success();
    }

}
